package com.groupfour.attendancemanagementsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupfour.attendancemanagementsystem.entity.DTO.*;
import com.groupfour.attendancemanagementsystem.entity.VO.CheckStudentVO;
import com.groupfour.attendancemanagementsystem.entity.VO.CourseVO;
import com.groupfour.attendancemanagementsystem.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import com.groupfour.attendancemanagementsystem.dao.CourseDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;

/**
 *
 */
@SuppressWarnings("ALL")
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;

    @Override
    public List<String> findCourseName() {
        return this.courseDao.findCourseNameByFormClick();
    }

    @Override
    public List<TeacherInfoDTO> findTrainTeacher() {
        return this.courseDao.findTrainTeacherByFormClick();
    }

    @Override
    public List<TeacherInfoDTO> findDutyTeacher() {
        return this.courseDao.findDutyTeacherByFormClick();
    }

    @Override
    public Map<String,Object> findCourseByCourseDTO(CourseDTO courseDTO,Integer pageNum,Integer pageSize) {

        Map<String,Object> map = new HashMap<>();

        //查询结果
        List<CourseVO> courseByCourseDTO = this.courseDao.findCourseByCourseDTO(courseDTO);
        //清理PageHelper缓存
        PageHelper.clearPage();
        //创建分页对象
        PageHelper.startPage(pageNum, pageSize);
        //创建分页信息对象
        PageInfo<CourseVO> pageInfo = new PageInfo<>(courseByCourseDTO);

        PageHelper.clearPage();
        List<CourseVO> result = pageInfo.getList();
        for (CourseVO courseVO : result) {
            courseVO.setTrainTeacher(this.courseDao.findTrainTeacherByCourseCode(courseVO.getCourseCode()));
            courseVO.setDutyTeacher(this.courseDao.findDutyTeacherByCourseCode(courseVO.getCourseCode()));
        }
        map.put("pageCount",pageInfo.getTotal());
        map.put("result",result);
        return map;
    }

    @Override
    public Map<String, Object> findAllCourse(Integer pageNum, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        //查询结果
        List<CourseVO> courseByCourseDTO = this.courseDao.findAllCourse();
        for (CourseVO courseVO : courseByCourseDTO) {
            courseVO.setTrainTeacher(this.courseDao.findTrainTeacherByCourseCode(courseVO.getCourseCode()));
            courseVO.setDutyTeacher(this.courseDao.findDutyTeacherByCourseCode(courseVO.getCourseCode()));
        }
        //清理PageHelper缓存
        PageHelper.clearPage();
        //创建分页对象
        PageHelper.startPage(pageNum, pageSize);
        //创建分页信息对象
        PageInfo<CourseVO> pageInfo = new PageInfo<>(courseByCourseDTO);
        List<CourseVO> result = pageInfo.getList();

        map.put("pageCount",pageInfo.getTotal());
        map.put("result",result);
        //将结果返回
        return map;
    }

    @Override
    @Transactional
    public Integer addCourse(CourseDTOByAdd courseDTOByAdd) {
        //查询课程名称是否存在
        String name = this.courseDao.findCourseName(courseDTOByAdd.getCourseName());
        if (name==null){
            name = "";
        }
        if (name.equals(courseDTOByAdd.getCourseName())){
            return 0;
        }
        //将课程名称存入课程名称表中
        this.courseDao.addProjectCourse(courseDTOByAdd.getCourseName());
        //将传入的TrainTeacher中与DutyTeacher清除
        if (courseDTOByAdd.getTrainTeacher().size()>1){
            for (String s : courseDTOByAdd.getTrainTeacher()) {
                if (s.equals(courseDTOByAdd.getDutyTeacher())){
                    courseDTOByAdd.getTrainTeacher().remove(s);
                }
            }
        }

        //获取当前时间戳为课程编号
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        String code = Long.toHexString(timestamp.getTime());
        courseDTOByAdd.setCourseCode(code);
        //添加课程表信息
        this.courseDao.addCourse(courseDTOByAdd);
        //根据老师姓名获取老师code
        List<String> trainTeacherCode = this.courseDao.findTrainTeacherCode(courseDTOByAdd.getTrainTeacher());
        String dutyTeacherCode = this.courseDao.findDutyTeacherCode(courseDTOByAdd.getDutyTeacher());
        //存入teacher_teach
        for (String teacherCode : trainTeacherCode) {
            this.courseDao.addTrainTeacher(teacherCode,code);
        }
        this.courseDao.addDutyTeacher(dutyTeacherCode,code);
        return 1;
    }

    @Override
    @Transactional
    public Integer updateCourse(CourseDTOByUpdate courseDTOByUpdate,String courseCode) {
        //根据老师姓名获取老师code
        List<String> trainTeacherCode = this.courseDao.findTrainTeacherCode(courseDTOByUpdate.getTrainTeacher());
        String dutyTeacherCode = this.courseDao.findDutyTeacherCode(courseDTOByUpdate.getDutyTeacher());
        //跟新course表
        this.courseDao.updateCourse(courseDTOByUpdate,courseCode);
        //跟新teacher_teach表
        for (String teacherCode : trainTeacherCode) {
            this.courseDao.updateTrainTeacher(teacherCode,courseCode);
        }
        this.courseDao.updateDutyTeacher(dutyTeacherCode,courseCode,new Date());
        return 1;
    }

    @Override
    public List<GroupInfoDTO> findGroupNames() {
        return this.courseDao.findGroupNames();
    }

    @Override
    public List<StudentInfoDTO> chooseStudent(String groupCode) {
        //查询该组的所有学生
        List<String> studnetCode = this.courseDao.findStudentCodeByGroupCode(groupCode);
        //返回全部学生
        List<StudentInfoDTO> result = this.courseDao.findStudentNameByCode(studnetCode);
        return result;
    }

    @Override
    public int addStudentInCourse(StudentInCourseDTO studentInCourseDTO) {
        return this.courseDao.addStudentInCourse(studentInCourseDTO);
    }

    @Override
    public Map<String, Object> findAllStudentCourse(String studentCode, Integer pageNum, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        //查询学生上课的课程
        List<String> courseCode = this.courseDao.findCourseCodeByStudentCode(studentCode);
        List<CourseVO> coursePO = new ArrayList<CourseVO>();
        for (String s : courseCode) {
            CourseVO allStudentCourse = this.courseDao.findAllStudentAndTeacherCourse(s);
            allStudentCourse.setDutyTeacher(this.courseDao.findDutyTeacherByCourseCode(s));
            allStudentCourse.setTrainTeacher(this.courseDao.findTrainTeacherByCourseCode(s));
            coursePO.add(allStudentCourse);
        }
        //清理PageHelper缓存
        PageHelper.clearPage();
        //创建分页对象
        PageHelper.startPage(pageNum, pageSize);
        //创建分页信息对象
        PageInfo<CourseVO> pageInfo = new PageInfo<>(coursePO);
        List<CourseVO> result = pageInfo.getList();

        map.put("pageCount",pageInfo.getTotal());
        map.put("result",result);
        return map;
    }

    @Override
    public Map<String, Object> findStudentCourse(String studentCode, Integer pageNum, Integer pageSize, CourseDTO courseDTO) {
        Map<String,Object> map = new HashMap<>();
        //查询学生上课的课程
        List<String> courseCode = this.courseDao.findCourseCodeByStudentCode(studentCode);
        List<CourseVO> list = this.courseDao.findStudentAndTeacherCourseByCourseDTO(courseDTO, courseCode);
        for (CourseVO courseVO : list) {
            courseVO.setTrainTeacher(this.courseDao.findTrainTeacherByCourseCode(courseVO.getCourseCode()));
            courseVO.setDutyTeacher(this.courseDao.findDutyTeacherByCourseCode(courseVO.getCourseCode()));
        }
        //清理PageHelper缓存
        PageHelper.clearPage();
        //创建分页对象
        PageHelper.startPage(pageNum, pageSize);
        //创建分页信息对象
        PageInfo<CourseVO> pageInfo = new PageInfo<>(list);
        List<CourseVO> result = pageInfo.getList();

        map.put("pageCount",pageInfo.getTotal());
        map.put("result",result);
        return map;
    }

    @Override
    public Map<String, Object> findAllTeacherCourse(String teacherCode, Integer pageNum, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        //查询老师上课的课程
        List<String> courseCode = this.courseDao.findCourseCodeByTeacherCode(teacherCode);
        List<CourseVO> coursePO = new ArrayList<CourseVO>();
        for (String s : courseCode) {
            CourseVO allStudentCourse = this.courseDao.findAllStudentAndTeacherCourse(s);
            allStudentCourse.setDutyTeacher(this.courseDao.findDutyTeacherByCourseCode(s));
            allStudentCourse.setTrainTeacher(this.courseDao.findTrainTeacherByCourseCode(s));
            coursePO.add(allStudentCourse);
        }
        //清理PageHelper缓存
        PageHelper.clearPage();
        //创建分页对象
        PageHelper.startPage(pageNum, pageSize);
        //创建分页信息对象
        PageInfo<CourseVO> pageInfo = new PageInfo<>(coursePO);
        List<CourseVO> result = pageInfo.getList();

        map.put("pageCount",pageInfo.getTotal());
        map.put("result",result);
        return map;
    }

    @Override
    public Map<String, Object> findTeacherCourse(String teacherCode, Integer pageNum, Integer pageSize, CourseDTO courseDTO) {
        Map<String,Object> map = new HashMap<>();
        //查询老师上课的课程
        List<String> courseCode = this.courseDao.findCourseCodeByTeacherCode(teacherCode);
        List<CourseVO> list = this.courseDao.findStudentAndTeacherCourseByCourseDTO(courseDTO, courseCode);
        for (CourseVO courseVO : list) {
            courseVO.setTrainTeacher(this.courseDao.findTrainTeacherByCourseCode(courseVO.getCourseCode()));
            courseVO.setDutyTeacher(this.courseDao.findDutyTeacherByCourseCode(courseVO.getCourseCode()));
        }
        //清理PageHelper缓存
        PageHelper.clearPage();
        //创建分页对象
        PageHelper.startPage(pageNum, pageSize);
        //创建分页信息对象
        PageInfo<CourseVO> pageInfo = new PageInfo<>(list);
        List<CourseVO> result = pageInfo.getList();

        map.put("pageCount",pageInfo.getTotal());
        map.put("result",result);
        return map;
    }

    @Override
    public List<String> findAllGroupName() {
        return this.courseDao.findAllGroupName();
    }

    @Override
    public List<String> findAllMajor() {
        return this.courseDao.findAllMajor();
    }

    @Override
    public List<String> findAllClasses() {
        return this.courseDao.findAllClasses();
    }

    @Override
    public List<CheckStudentVO> checkAllStudent(String courseCode,CheckStudentDTO checkStudentDTO) {
        return this.courseDao.findThisCourseStudent(courseCode,checkStudentDTO);
    }

    @Override
    public Integer deleteCourse(String courseCode) {
        return this.courseDao.updateCourseDeleteStatus(courseCode);
    }
}




